package com.bawei.live.view

import android.view.MotionEvent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.RoutePath
import com.bawei.common.base.BaseActivity
import com.bawei.live.R
import com.bawei.live.adapter.LivePageAdapter
import com.bawei.live.databinding.ActivityZhiboBinding
import com.bawei.live.entity.TabEntity
import com.bawei.live.fragment.GlobalFlashFragment
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import dagger.hilt.android.AndroidEntryPoint

/**
 * Desc: 直播间,实现全站和带货榜fragment切换
 */
@Route(path = RoutePath.PATH_ZHIBO_PAGE)
@AndroidEntryPoint
class ZhiboActivity : BaseActivity<ActivityZhiboBinding>() {

    private var tabList = arrayListOf<CustomTabEntity>(TabEntity("全站",0,0))
    private var fragments = arrayListOf<Fragment>()

    private lateinit var livepageAdapter:LivePageAdapter

    override fun getLayoutId(): Int = R.layout.activity_zhibo

    override fun initViews() {

        //点击屏幕来进行关闭
        mDataBinding.main.setOnTouchListener{v,event->
            if (event.action == MotionEvent.ACTION_DOWN) {
                mDataBinding.cltPlacard.visibility = View.GONE
            }
            true
        }

        mDataBinding.tvHours.setOnClickListener{
            mDataBinding.cltPlacard.visibility = View.VISIBLE
        }



        tabList.add(TabEntity("带货榜",0,0))
        mDataBinding.commonTableLayout.setTabData(tabList)

        fragments.add(ARouter.getInstance().build(RoutePath.PATH_FRAGMENT_GLOBAL_COMMEND).navigation() as Fragment)
        fragments.add(ARouter.getInstance().build(RoutePath.PATH_FRAGMENT_GOODS_SALE_COMMEND).navigation() as Fragment)

        livepageAdapter = LivePageAdapter(fragments,supportFragmentManager,lifecycle)


        mDataBinding.vp2Fragment.apply {
            offscreenPageLimit = 1
            adapter = livepageAdapter
        }
        //CommonLayout和ViewPage2进行联动
        mDataBinding.commonTableLayout.setOnTabSelectListener(object:OnTabSelectListener{
            override fun onTabSelect(position: Int) {
                mDataBinding.vp2Fragment.currentItem = position
            }

            override fun onTabReselect(position: Int) {

            }
        })

        mDataBinding.vp2Fragment.registerOnPageChangeCallback(object:ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                mDataBinding.commonTableLayout.currentTab = position
            }
        })



    }

}